package com.qzxy.premiumhotelsystem.controller.common;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import com.qzxy.premiumhotelsystem.common.constant.UserRoleConstant;
import com.qzxy.premiumhotelsystem.common.result.Result;
import com.qzxy.premiumhotelsystem.service.LogsService;
import com.qzxy.premiumhotelsystem.service.RoomTypesService;
import com.qzxy.premiumhotelsystem.service.VipTypesService;
import com.qzxy.premiumhotelsystem.utils.AliOSSUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;

import static cn.dev33.satoken.SaManager.log;

/**
 * @Author: iQNRen
 * @Description: 图片接口
 * @Date: 2024/9/18 20:02 周三
 * @Version: 1.0
 */
@RestController
//@SaCheckLogin
@Tag(name = "通用接口")
@RequestMapping("/file")
public class FileController {
    @Resource
    private AliOSSUtils aliOSSUtils;
    @Resource
    private LogsService logsService;
    @Resource
    private VipTypesService vipTypesService;

    @Value("${user.alioss.endpoint}")
    String endpoint;
    @Value("${user.alioss.bucket-name}")
    String bucketName;

    @Resource
    private RoomTypesService roomTypesService;

    /**
     * oss文件上传
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    @SaCheckLogin
    public Result uploadByAliOOS(MultipartFile file) throws Exception {
        log.info("文件上传, 文件名: {}", file.getOriginalFilename());

        String url = aliOSSUtils.upload(file);
        String fileName = url.substring(url.lastIndexOf('/') + 1);
        log.info("文件上传完成, 文件名:{},文件访问的url: {}",fileName, url);

//        logsService.create("文件上传完成");
        // 将用户头像地址写入数据库 并返回给前端
        return Result.ok(url);
    }

    /**
     * oss删除文件
     * @param fileName
     * @return
     */
    @DeleteMapping("/delete/{fileName}")
    @Operation(summary = "文件删除")
    @SaCheckRole(type = UserRoleConstant.ADMIN,value = UserRoleConstant.ADMIN)
    public Result deleteByFileName(@PathVariable String fileName) {
        System.out.println("删除的文件为: " + fileName);
        Integer code = aliOSSUtils.delFile(fileName);
        if (code == 200) return Result.ok("删除成功");
        else return Result.error("删除失败");
    }

    /**
     * 访问图片
     * @param fileName
     * @return
     * @throws IOException
     */
    @GetMapping("/{fileName}")
    @Operation(summary = "文件访问")
    @SaIgnore
    public Result get(@PathVariable String fileName) throws IOException {
        // 文件访问路径
        String url = "https://"+ bucketName + "." + endpoint + "/" + fileName;
        // 把上传到oss的路径返回
        return Result.ok(url);
    }


    /**
     * 当修改相关的装配信息，可手动删除缓存，并重新装配
     * @return
     */
    @Operation(description = "装配相关信息到redis以防mysql压力大")
    @PostMapping("/armory")
    public Result armoryMessage(){

        vipTypesService.armoryVipTypesMes();
        roomTypesService.armoryRoomTypesMes();
        return Result.ok("已装配vip_types、room_types相关信息");
    }
}

